package github.sf.fw.tools.db.catalog;

import lombok.Data;

/**
 * @author zhoup
 * @since 2023/7/26
 */
@Data
public class ColumnModel implements Fragment {
    /**
     * 列名
     */
    protected String columnName;
    /**
     * 列类型
     */
    protected String columnType;
    /**
     * 列约束
     */
    protected String columnConstraint;
    /**
     * 默认值
     */
    protected String defaultValue;
    /**
     * 列注释
     */
    protected String columnComment;
    /**
     * 列是否为空值
     */
    protected boolean nullValue;

    @Override
    public String getFragment() {
        emptyVerify(columnName, "列名不能为空");
        emptyVerify(columnType, "列类型不能为空");
        StringBuilder result = new StringBuilder(columnName + " " + columnType);
        addPublicPart(result);
        return result.toString();
    }

    protected void addPublicPart(StringBuilder result) {
        if (columnConstraint != null && !columnConstraint.isEmpty()) {
            result.append(" ").append(columnConstraint);
        }
        if (defaultValue != null && !defaultValue.isEmpty()) {
            result.append(" ").append("DEFAULT ").append(defaultValue);
        }
        if (columnComment != null && !columnComment.isEmpty()) {
            result.append(" ").append("COMMENT ").append("\"").append(columnComment).append("\"");
        }
    }

    protected void emptyVerify(String fieldValue, String message) {
        if (fieldValue == null || fieldValue.isEmpty()) {
            throw new IllegalArgumentException(message);
        }
    }
}
